#!/usr/bin/python
# -*- coding: utf-8 -*-

import Ice
import sys, os, random, time

try:
    HOME = os.environ['ROBOTS']
except KeyError:
    print 'ROBOTS environment variable is not currently defined.'
    sys.exit(0)

SLICE = os.path.join(HOME, 'slice')
FILE = os.path.join(SLICE, 'Robots.ice')
Ice.loadSlice('--all -I/usr/share/slice', ['-I' + SLICE, FILE])

import Robots

ROBOTS = ['192.168.1.9']
ACTIONS = 5

class Client (Ice.Application):

    def run (self, argv):

        factory = Robots.RobotFactoryPrx.checkedCast(
            self.communicator().stringToProxy('RobotFactory -t:tcp -h 192.168.1.22 -p 9898'))

        prxs = factory.create(ROBOTS)

        time.sleep(1)

        for i in range (0, ACTIONS):
            for prx in prxs:

                r = random.Random().randint(0, 2)

                if r == 0: # Move.
                    prx.move(Robots.RobotPos(Robots.Vec2(0, 0),
                                             float(45)))
                elif r == 1: # Left.
                    prx.turn(Robots.Direction.DLeft,
                             45)
                elif r == 2: # Right.
                    prx.turn(Robots.Direction.DRight,
                             45)

        time.sleep(2)

        for prx in prxs:
            print 'Destroying ' + str(prx)
            prx.destroy()

        return 0

Client().main(sys.argv, os.path.join(HOME, 'config', 'config.client'))
